<?php

class DealersHelper extends Helper {

    private $_dealersfield;
    private $dealers_rule;

    public function __construct() {
        $this->_dealersfield = array('dl_id', 'dl_name', 'dl_abb', 'dl_tel', 'dl_brid');
        $this->dealers_rule = array(
            'dl_name' => array('require', '经销商名称不能为空'),
            'dl_brid' => array('require', '所属网点不能为空')
        );
        
    }
    /**
     * 
     * @return type
     */
    public function dealersSave($data, $where = ''){
        if(is_numeric($where)){
            $where = array('`dl_id` = ?' => $where);
        }
        $validator = Validator::checkRule($data, $this->dealers_rule);
        if(!$validator['pass']){
            return array('success' => false, 'msg' => $validator['msg']);
        }
        $dealersModel = $this->LoadModel('Public'); 
        if(empty($where)){
            $id = $dealersModel->save('dealers', $data);
            if($id){
                return array('success' => true, 'msg' => $id);
            }else{
                return array('success' => false, 'msg' => '保存失败');
            }
        }else{
            $dealersModel->update('dealers', $data, $where);
            return array('success' => true);
        }
    }
    /**
     * 
     * @param type $where
     * @param type $page
     * @param type $limit
     * @param type $param
     * @return type
     */
    public function dealersList($where, $page = 1, $limit = PAGE_SIZE, $param = array()){
        $dealersModel = $this->LoadModel('Public'); 
        $data['count'] = $dealersModel->getRowCount('dealers', $where);
        if(!empty($data['count'])){
            $data['all'] = $dealersModel->getList('dealers', $this->_dealersfield, $where, array($page, $limit), NULL, array('dl_id DESC'));
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
        }
        return $data;
    }
    
    public function dealersRow($where){
        if(is_numeric($where)){
            $where = array('`dl_id` = ?' => $where);
        }
        $dealersModel = $this->LoadModel('Public'); 
        $data = $dealersModel->getOneRow('dealers', $this->_dealersfield, $where);
        return $data;
    }
    
    public function dealersRemove($where){
        if(is_numeric($where)){
            $where = array('`dl_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public'); 
        $do = $model->update('dealers', array('dl_status' => 'del'), $where);
        return $do;
    }
    /**
     * 
     * @param type $where
     * @return type
     */
    public function dealersAll($where){
        if(is_numeric($where)){
            $where = array('`dl_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public');
        $data = $model->getList('dealers', $this->_dealersfield, $where);
        return $data;
    }
}